/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.jrk_verwaltung.security.user;

import de.jrk_verwaltung.design.personen.CPerson;
import de.jrk_verwaltung.exceptions.NonexistentEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 * 
 * @author JRK-FO
 */
public class CUserController implements Serializable {

    /**
	 * 
	 */
    private static final long serialVersionUID = 1L;

    public CUserController(final EntityManagerFactory emf) {
        this.emf = emf;
    }

    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(CUser jrkUser) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CPerson fkIdperson = jrkUser.getFkIdperson();
            if (fkIdperson != null) {
                fkIdperson = em.getReference(fkIdperson.getClass(), fkIdperson.getIdperson());
                jrkUser.setFkIdperson(fkIdperson);
            }
            em.persist(jrkUser);
            if (fkIdperson != null) {
                fkIdperson.getJrkUserCollection().add(jrkUser);
                fkIdperson = em.merge(fkIdperson);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CUser jrkUser) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CUser persistentJrkUser = em.find(CUser.class, jrkUser.getIduser());
            CPerson fkIdpersonOld = persistentJrkUser.getFkIdperson();
            CPerson fkIdpersonNew = jrkUser.getFkIdperson();
            if (fkIdpersonNew != null) {
                fkIdpersonNew = em.getReference(fkIdpersonNew.getClass(), fkIdpersonNew.getIdperson());
                jrkUser.setFkIdperson(fkIdpersonNew);
            }
            jrkUser = em.merge(jrkUser);
            if (fkIdpersonOld != null && !fkIdpersonOld.equals(fkIdpersonNew)) {
                fkIdpersonOld.getJrkUserCollection().remove(jrkUser);
                fkIdpersonOld = em.merge(fkIdpersonOld);
            }
            if (fkIdpersonNew != null && !fkIdpersonNew.equals(fkIdpersonOld)) {
                fkIdpersonNew.getJrkUserCollection().add(jrkUser);
                fkIdpersonNew = em.merge(fkIdpersonNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = jrkUser.getIduser();
                if (findJrkUser(id) == null) {
                    throw new NonexistentEntityException("The jrkUser with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CUser jrkUser;
            try {
                jrkUser = em.getReference(CUser.class, id);
                jrkUser.getIduser();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The jrkUser with id " + id + " no longer exists.", enfe);
            }
            CPerson fkIdperson = jrkUser.getFkIdperson();
            if (fkIdperson != null) {
                fkIdperson.getJrkUserCollection().remove(jrkUser);
                fkIdperson = em.merge(fkIdperson);
            }
            em.remove(jrkUser);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CUser> findJrkUserEntities() {
        return findJrkUserEntities(true, -1, -1);
    }

    public List<CUser> findJrkUserEntities(int maxResults, int firstResult) {
        return findJrkUserEntities(false, maxResults, firstResult);
    }

    @SuppressWarnings("unchecked")
    private List<CUser> findJrkUserEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery<Object> cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(CUser.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            if (q != null) {
                return q.getResultList();
            } else {
                return null;
            }
        } finally {
            em.close();
        }
    }

    public CUser findJrkUser(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CUser.class, id);
        } finally {
            em.close();
        }
    }

    public int getJrkUserCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery<Object> cq = em.getCriteriaBuilder().createQuery();
            Root<CUser> rt = cq.from(CUser.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
